package com.yztc.xiami_app.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.yztc.xiami_app.bean.ssq.Address;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 周旭 on 2016/11/18.
 * "省市区"地址管理的工具
 */

public class AddressManager {

    //ssq表的字段的名字
    private static final String TABLE_NAME = "ssq"; //表名
    private static final String PNAME = "pName"; //省份名称
    private static final String CNAME = "cName"; //市的名称
    private static final String AREA = "area"; //区的名称

    private DBHelper dbHelper;

    public AddressManager(Context context) {
        dbHelper = new DBHelper(context);
    }


    //批量插入
    public boolean insert(List<Address> list) throws Exception{
        //打开数据库
        //如果数据库不存在，则创建并打开。否则，直接打开。
        SQLiteDatabase db = dbHelper.getReadableDatabase();

        db.beginTransaction();
        for (int i=0;i<list.size();i++){
            //数据添加的操作
            ContentValues cv = new ContentValues();
            cv.put(PNAME,list.get(i).getpName());
            cv.put(CNAME,list.get(i).getcName());
            cv.put(AREA,list.get(i).getAreaName());
            long id = db.insert(TABLE_NAME, null, cv);
            if(id != -1){
                Log.e("tag","------------->插入数据成功");
            }
        }
        db.setTransactionSuccessful();
        db.endTransaction();
        //关闭数据库
        db.close();

        return true;
    }

    /**查询指定省份或直辖市的所有市的集合
     * @param provinceName：省份或直辖市的名称
     * @return
     */
    public List<Address> getCities(String provinceName){
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        //String sql = "select distinct cName from ssq where pName = ?";
        String sql = "select distinct "  + CNAME + " from " + TABLE_NAME + " where " + PNAME + " = ?";
        Cursor cursor = db.rawQuery(sql, new String[]{provinceName});
        List<Address> addressList = new ArrayList<>();
        while (cursor.moveToNext()){
            String cName  = cursor.getString(cursor.getColumnIndex(CNAME));
            Log.i("tag","----->"+cName);
            Address address = new Address(null,cName,null);
            addressList.add(address);
        }
        //关闭游标
        cursor.close();
        //关闭数据库
        db.close();
        return addressList;
    }

    /**
     * 查询指定市的所有区的列表的List集合
     * @param cityName
     * @return
     */
    public List<Address> getAreas(String cityName){
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        //获取指定市的所有区的列表的sql语句
        String sql = "select distinct "  + AREA + " from " + TABLE_NAME + " where " + CNAME + " = ?";
        Cursor cursor = db.rawQuery(sql, new String[]{cityName});
        List<Address> addressList = new ArrayList<>();
        while (cursor.moveToNext()){
            //获取区的名称
            String area  = cursor.getString(cursor.getColumnIndex(AREA));
            Log.i("tag","----->"+area);
            Address address = new Address(null,null,area);
            //把所有的区添加到List集合
            addressList.add(address);
        }
        //关闭游标
        cursor.close();
        //关闭数据库
        db.close();
        return addressList;
    }

    //查询所有
    public List<Address> getAll(){
        SQLiteDatabase db = dbHelper.getReadableDatabase();
        String sql = "select * from " + TABLE_NAME;
        Cursor cursor = db.rawQuery(sql, null);
        List<Address> addressList = new ArrayList<>();
        while (cursor.moveToNext()){
            String pName  = cursor.getString(cursor.getColumnIndex(PNAME));
            String cName  = cursor.getString(cursor.getColumnIndex(CNAME));
            String area  = cursor.getString(cursor.getColumnIndex(AREA));
            Address address = new Address(pName,cName,area);
            addressList.add(address);
        }
        //关闭游标
        cursor.close();
        //关闭数据库
        db.close();
        return addressList;

    }

}
